<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <script>
    function getNumber() {
      var p = new Promise(function (resolve, reject) {
        //做一些异步操作
        setTimeout(function () {
          var num = Math.ceil(Math.random() * 10); //生成1-10的随机数
          if (6 <= 5) {
            resolve(num);
          } else {
            reject('数字太大了');
          }
        }, 2000);
      });
      return p;
    }

    getNumber()
      .then(
        function (data) {
          console.log('resolved');
          console.log(data);
        },
        function (reason, data) {
          console.log('rejected2');
          console.log(reason);
        }
      ).catch(function (reason) {
        console.log('rejected3');
        console.log(reason);
      });
  </script>
</body>

</html>